<?php
require_once("../DAO/QuestionDAO.php");
require_once("../DAO/AnswerDAO.php");
require_once("../Entities/Question.php");
require_once("../Entities/Answer.php");
require_once("../DAO/ParagraphDAO.php");

// 0 : get all answers of question
// 1 : get answer by id
// 2 : add answer
// 3 : update answer
// 4 : delete answer
// 5 : get content question
// 6 : get paragraph content

if(isset($_REQUEST["type"]))
{
	$type = $_REQUEST["type"];
	$questionDao = new QuestionDAO();
	$answerDao = new AnswerDAO();
	if($type == 0)
	{
		$id = $_REQUEST["id"]; // question id
		$arrayKq = $answerDao->GetAnswersByQuestionId($id);
		echo json_encode($arrayKq);
	}
	else if($type == 1)
	{
		$id = $_REQUEST["id"]; // answer id
		$kq = $answerDao->GetAnswerById($id);
		echo json_encode($kq);
	}
	else if($type == 2)
	{
		$item = new Answer();
		$item->Content = $_REQUEST["content"];
		$item->IsCorrect = $_REQUEST["correct"];
		$item->Sentence = 1;
		$item->Question = $_REQUEST["question"];
		$answerDao->CreateAnswer($item);
		$arrayKq = array();
		$arrayKq["result"] = 1;
		echo json_encode($arrayKq);
	}
	else if($type == 3)
	{
		$item = $answerDao->GetAnswerById($_REQUEST["id"]);
		$arrayKq = array();
		if($item != null)
		{
			$item->Content = $_REQUEST["content"];
			$item->IsCorrect = $_REQUEST["correct"];
			$item->Sentence = 1;
			$item->Question = $_REQUEST["question"];
			$answerDao->UpdateAnswer($item);
			$arrayKq["result"] = 1;
		}
		else
		{
			$arrayKq["result"] = 0;
		}
		echo json_encode($arrayKq);
	}
	else if($type == 5)
	{
		$id = $_REQUEST["id"]; // question id
		$item = $questionDao->GetQuestionById($id);
		if($item == null)
		{
			$arrayKq["result"] = "Content not found";
		}
		else
		{
			$arrayKq["result"] = $item->Content;
		}
		echo json_encode($arrayKq);
	}
	else if($type == 6)
	{
		$id = $_REQUEST["id"]; // paragraph id
		$paragraphDAO = new ParagraphDAO();
		$item = $paragraphDAO->GetParagraphById($id);
		if($item == null)
		{
			$arrayKq["result"] = "Content not found";
		}
		else
		{
			$arrayKq["result"] = $item->Content;
		}
		echo json_encode($arrayKq);
	}
}

?>